﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NigiriClub.WebUI.Models;
using NigiriClub.WebUI.Models.PagedCollections;

namespace NigiriClub.WebUI.Controllers
{
    public class GamesController : Controller
    {
        //
        // GET: /Games/

        public ActionResult Index(int page = 1)
        {
            return View(new GamesModel(page));
        }

        public ActionResult Games(bool isReviewed = true)
        {
            return PartialView("_GameTable", new GamesModel(1, isReviewed));
        }

        public ActionResult Show(int id)
        {
            Game game;
            using (Entities db = new Entities())
            {
                game = db.Game.FirstOrDefault(g => g.Id == id);
            }
            return View(game);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadSgf(Game
            game)
        {

            string fileRelativePath = String.Format("/Content/{0}/{1}-{2}",
                "games", Guid.NewGuid().ToString().Substring(0, 13), game.File.file.FileName);
            string filePath = Server.MapPath(fileRelativePath);

            game.File.file.SaveAs(filePath);
            game.Path = fileRelativePath;

            using (Entities db = new Entities())
            {
                Account account = db.Account.FirstOrDefault(u => u.Name == User.Identity.Name);
                game.OwnerId = account.Id;
                db.Game.Add(game);
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadReviewedSgf(FileModel game)
        {
            string fileRelativePath = String.Format("/Content/{0}/{1}-{2}",
                "games", Guid.NewGuid().ToString().Substring(0, 13), game.file.FileName);
            string filePath = Server.MapPath(fileRelativePath);
            game.file.SaveAs(filePath);

            using (var db = new Entities())
            {
                var account = db.Account.FirstOrDefault(u => u.Name == User.Identity.Name);
                var reviewed = db.Game.FirstOrDefault(n => n.ReviewerId == account.Id && !n.IsReviewed);
                reviewed.IsReviewed = true;
                reviewed.Path = fileRelativePath;
                db.SaveChanges();

            }

            return RedirectToAction("Index");
        }

        public ActionResult DeleteGame(int id)
        {
            using (Entities db = new Entities())
            {
                db.Game.Remove(db.Game.FirstOrDefault(n => n.Id == id));
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        public ActionResult Review(int id)
        {
            using (Entities db = new Entities())
            {
                Game game = db.Game.FirstOrDefault(n => n.Id == id);
                Account account = db.Account.FirstOrDefault(u => u.Name == User.Identity.Name);
                game.ReviewerId = account.Id;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
    }
}
